﻿using System;
using System.Windows.Input;

namespace MC.Warehouse.Core
{
	public class RelayCommand:ICommand
	{
		#region Fields

        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;

        #endregion // Fields

        #region Constructors

        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        public RelayCommand()
        {
            // TODO: Complete member initialization
        }

        #endregion // Constructors

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged = delegate { };

        public void Execute(object parameter)
        {
            //WaitingProcess.Show();
            _execute(parameter);
            //WaitingProcess.Close();
        }

        #endregion // ICommand Members

        //#region Event

        //public event Action<object> ExecuteTargets
        //{
        //    add
        //    {
        //        _execute = value;
        //    }
        //    remove
        //    {
        //        _execute -= value;
        //    }
        //}

        //public event Predicate<object> CanExecuteTargets
        //{
        //    add
        //    {
        //        _canExecute = value;
        //        CanExecuteChanged(this, EventArgs.Empty);
        //    }
        //    remove
        //    {
        //        _canExecute -= value;
        //        CanExecuteChanged(this, EventArgs.Empty);
        //    }
        //}
        //#endregion
    }
}